package org.dream.ui.chart.impl;

import java.math.BigDecimal;

import org.dream.ui.chart.YAxisModel;
import org.dream.util.DoubleUtil;

/**
 * 
 * @author qiuriyuchen E-mail:panpan_001@126.com
 */
public class YAxisModelImpl extends AxisModelImpl implements YAxisModel {

	public void adjustmentMaxAndMinVale() {
		if (minValue >= 0 && maxValue <= 1) {
			minValue = DoubleUtil.changeDecimal(minValue, 1, BigDecimal.ROUND_DOWN);
			maxValue = DoubleUtil.changeDecimal(maxValue, 1, BigDecimal.ROUND_UP);

		}

	}

	public double getTickDistPixel() {
		// 计算每个刻度的像素值
		return Math.abs((double) (this.getHeight()) / (double) (getTickCount()));
	}

	public int translateValueToPx(double trueValue) {
		int screenRangeValue = (int) ((trueValue - minValue) * this.getHeight() / getRangeValue());
		return (getPixelYButtom() - screenRangeValue);
	}

	public double translatePxToValue(int screenValue) {
		double trueValue = (((screenValue - getPixelYButtom()) * getRangeValue()) / this.getHeight());
		return minValue - trueValue;
	}

	public int getTickPixelY(int i) {
		return (int) (this.getPixelYButtom() - this.getTickDistPixel() * i);
	}

	@Override
	public int getPixelYButtom() {
		return this.getPixelYTop() + this.getHeight();
	}

	@Override
	public int getPixelYTop() {
		if (index == 1) {
			return getTopMargin();
		} else {
			return getTopMargin() + (chartModel.getUseableHeight() - this.getHeight());
		}

	}

	public int getStartPointYCoordinatePixel() {
		return this.getPixelYTop();
	}

	public int getEndPointYCoordinatePixel() {
		return getPixelYButtom();
	}

	public boolean isdrawButtomLine() {
		return chartModel.getWindowCount() > 1 && getIndex() < chartModel.getWindowCount();
	}

	public void beforeDraw() {
		// 设置y坐标轴高度
		this.setHeight((int) (chartModel.getUseableHeight() * this.heightScan));
		this.setTopMargin(chartModel.getTopMargin());

	}

	private double minValue = Double.MAX_VALUE;// 最小值
	private double maxValue = -Double.MAX_VALUE;// 最大值
	protected int tickCount = 10;// 刻度数量
	private int height;
	private int topMargin;
	private Integer index;
	private Double heightScan;

	public YAxisModelImpl(int tickCount, Double heightScan) {
		this.tickCount = tickCount;
		this.heightScan = heightScan;
	}

	public double getRangeValue() {
		return Math.abs(maxValue - minValue);
	}

	public double getMinValue() {
		return minValue;
	}

	public void setMinValue(double minValue) {
		this.minValue = minValue;
	}

	public double getMaxValue() {
		return maxValue;
	}

	public void setMaxValue(double maxValue) {
		this.maxValue = maxValue;
	}

	public double getTickDist() {
		return getRangeValue() / getTickCount();// 每个刻度的值
	}

	public int getTickCount() {
		return tickCount;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getHeight() {
		return height;
	}

	public void setTopMargin(int topMargin) {
		this.topMargin = topMargin;
	}

	public int getTopMargin() {
		return topMargin;
	}

	public void setIndex(Integer index) {
		this.index = index;
	}

	public Integer getIndex() {
		return index;
	}
}
